import Actions from "../actions/Actions";
import { ext } from "../extend/Extend";

export default class PopUpManager {
    public static Show_ScaleEffect(node, duration) {
        if (duration == undefined) {
            duration = 0.5;
        }
        node.active = true;

        var oldScaleX = 1;
        var oldScaleY = 1;

        node.setScale(0.1, 0.1);

        var action1 = cc.scaleTo(duration, oldScaleX, oldScaleY);
        // var action2 = new cc.scaleTo(duration, 1, 1);
        node.runAction(cc.sequence(action1, cc.callFunc(function () {
            node.setScale(oldScaleX, oldScaleY);
        })));
    }

    public static Hide_ScaleEffect(node, duration) {
        if (duration == undefined) {
            duration = 0.2;
        }

        var action1 = cc.scaleTo(duration, 0, 0);
        node.runAction(cc.sequence(action1, cc.callFunc(function () {
            node.active = false;
        })));
    }

    public static Remove_ScaleEffect(node, duration, releaseRes?: string) {
        if (duration == undefined) {
            duration = 0.2;
        }

        var action1 = cc.scaleTo(duration, 0, 0);
        node.runAction(cc.sequence(action1, cc.callFunc(function () {
            if (node.isValid) {
                node.destroy();
                releaseRes && cc.loader.releaseRes(releaseRes);
            }
        })));
    }


    private static _textTipsPrefab: cc.Prefab = null;

    static async initTextTips(textTipsPrefabName: string) {
        this._textTipsPrefab = await cc.loader.loadResAwait(textTipsPrefabName, cc.Prefab);
    }

    /** 文字提示 */
    public static tipTimeout: number = 0;
    static async textTips(parent: cc.Node, text: string, position?: cc.Vec2, fontSize?: number) {
        if (this._textTipsPrefab == null) {
            console.error("please call Facade.initTextTips before call Facade.textTips");
            return;
        }
        if (new Date().getTime() - this.tipTimeout < 2000) {
            return;
        }
        this.tipTimeout = new Date().getTime();

        let node = cc.instantiate(this._textTipsPrefab);
        node.setParent(parent);
        node.zIndex = cc.macro.MAX_ZINDEX;
        if (position) {
            node.position = position;
        } else {
            node.position = cc.v2(0, 220);
        }
        let richText = node.getComponent(cc.Label);
        if (fontSize) {
            richText.fontSize = fontSize;
        }
        richText.string = text;
        // ext.showRichText(richText);
        await node.runActionAwait(Actions.flutterAction());
        node.destroy();
    }
}
